/*
 * 文  件   名: FileWidget.java
 * 版         本 : docrepository-web(Anttribe). All rights reserved.
 * 描         述 : <描述>
 * 修   改  人: zhaoyong
 * 修改时间: 2014年10月29日
 */
package org.anttribe.docrepository.web.file.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.anttribe.docrepository.common.UnifyException;
import org.anttribe.docrepository.common.constants.Keys;
import org.anttribe.docrepository.file.domain.FileInfo;
import org.anttribe.docrepository.file.manager.FileInfoManager;
import org.anttribe.docrepository.user.domain.UserInfo;
import org.anttribe.docrepository.web.file.vo.FileInfoVo;
import org.anttribe.saturn.common.utils.ObjectUtils;
import org.anttribe.saturn.runtime.stereotype.Widget;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 用户个人文件Widget
 * 
 * @author zhaoyong
 * @version 2014年10月29日
 */
@Widget(name = "UserFilesWidget")
@RequestMapping("/user/files")
public class UserFilesWidget
{
    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(UserFilesWidget.class);
    
    /**
     * fileManager
     */
    @Autowired
    private FileInfoManager fileInfoManager;
    
    /**
     * 文件列表
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param httpSession HttpSession
     * @return ModelAndView
     */
    @RequestMapping("/list")
    public ModelAndView doUserFilesList(HttpServletRequest request, HttpServletResponse response,
        HttpSession httpSession)
    {
        ModelAndView mv = new ModelAndView();
        
        // 从session中获取用户
        UserInfo userInfo = (UserInfo)httpSession.getAttribute(Keys.USER_SESSION);
        if (!ObjectUtils.isNullOrNone(userInfo))
        {
            // 用户文件列表
            List<FileInfo> userFileList = null;
            try
            {
                userFileList = fileInfoManager.listFileByParent(request.getParameter("parentId"), userInfo.getUserId());
            }
            catch (UnifyException e)
            {
                LOGGER.warn("While getting user files, get error: {}", e.getErrorNo());
                mv.addObject("errorNo", e.getErrorNo());
            }
            mv.addObject("userFiles", userFileList);
        }
        
        mv.setViewName("file/userFiles");
        
        return mv;
    }
    
    /**
     * 获取用户收藏的文件列表
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param httpSession HttpSession
     * @return ModelAndView
     */
    @RequestMapping("/favorite")
    public ModelAndView doUserFavoriteFilesList(HttpServletRequest request, HttpServletResponse response,
        HttpSession httpSession)
    {
        ModelAndView mv = new ModelAndView();
        
        // 从session中获取用户
        UserInfo userInfo = (UserInfo)httpSession.getAttribute(Keys.USER_SESSION);
        if (ObjectUtils.isNullOrNone(userInfo))
        {
            LOGGER.warn("Can not find userinfo in session, return to signin.");
            mv.setViewName("signin/signin");
            return mv;
        }
        
        // 用户文件列表
        List<FileInfo> userFileList = null;
        try
        {
            userFileList = fileInfoManager.listFavoriteFileByUser(userInfo.getUserId());
        }
        catch (UnifyException e)
        {
            LOGGER.warn("While getting user favorite files, get error: {}", e.getErrorNo());
            mv.addObject("errorNo", e.getErrorNo());
        }
        mv.addObject("userFavorites", userFileList);
        mv.setViewName("file/userFavorites");
        
        return mv;
    }
    
    /**
     * 添加或修改文件
     * 
     * @return String
     */
    @RequestMapping("/edit")
    public String userFileEdit()
    {
        return "file/userFileEdit";
    }
    
    /**
     * 处理文件保存或更新
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param httpSession HttpSession
     * @param fileInfoVo FileInfoVo
     * @return ModelAndView
     */
    @RequestMapping("/doUserFileEdit")
    public ModelAndView doUserFileEdit(HttpServletRequest request, HttpServletResponse response,
        HttpSession httpSession, @ModelAttribute
        FileInfoVo fileInfoVo)
    {
        ModelAndView mv = new ModelAndView();
        
        // 从session中获取用户
        UserInfo userInfo = (UserInfo)httpSession.getAttribute(Keys.USER_SESSION);
        if (!ObjectUtils.isNullOrNone(userInfo))
        {
            try
            {
                fileInfoVo.setOwner(userInfo);
                
                System.out.println(fileInfoVo.getFileId());
                
                this.fileInfoManager.saveOrUpdateFileInfo(fileInfoVo);
                mv.setViewName("user/files");
            }
            catch (UnifyException e)
            {
                LOGGER.warn("While getting user files, get error: {}", e.getErrorNo());
                mv.addObject("errorNo", e.getErrorNo());
                mv.setViewName("/user/files/edit");
            }
        }
        
        return mv;
    }
}